package com.frkj.yuan.fragment.home.activity;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.activity.UserLoginActivity;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.YuyueKeshiBean;
import com.frkj.yuan.api.category.YuyueKeShiCategory;
import com.frkj.yuan.api.presenter.YuyueKeShiPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.dialog.LoginOutDialog;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 作者：菲菲 on 2022/2/15 09:43
 * company：丰蓉信息科技有限公司
 */
public class YuyueKeShiActivity extends BaseActivity implements YuyueKeShiCategory.View {
    @BindView(R.id.recycler_left)
    RecyclerView mLeftRecyclerView;
    @BindView(R.id.recycler_right)
    RecyclerView mRightRecyclerView;
    @BindView(R.id.base_title)
    TextView mTitleMsga;
    private long hospitalId;
    private YuyueKeShiPresenter mPresenter;
    private BaseRecyclerRefreshAdapter<YuyueKeshiBean.YuyueKeshiBeanData> adapter;
    private BaseRecyclerRefreshAdapter<YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital> mAdapter;
    private List<YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital> mKeshilist;
    private String mKeshiName;
    private String mKeshi_Name;
    private LoginOutDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_yuyue_keshi;
    }

    @Override
    public void initLayoutView() {
        mTitleMsga.setText(getIntent().getStringExtra("yiyuan"));
        //设置状态栏为黑色
        YCAppBar.setStatusBarLightMode(YuyueKeShiActivity.this, ContextCompat.getColor(YuyueKeShiActivity.this, R.color.white_color));
        RecyclerViewLayoutManager.setLinearVerticalManager(YuyueKeShiActivity.this, mLeftRecyclerView);
        RecyclerViewLayoutManager.setLinearVerticalManager(YuyueKeShiActivity.this, mRightRecyclerView);
        hospitalId = getIntent().getLongExtra("hospitalId", 0);
        Log.e("hospitalId009", hospitalId + "");
        mKeshiName = getIntent().getStringExtra("keshi");
        mPresenter = new YuyueKeShiPresenter(this);
        SetPersenter(mPresenter);
        JSONObject result = new JSONObject();
        try {
            result.put("hospitalId", hospitalId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mPresenter.getYuyueKeShiList(result.toString());
    }

    @OnClick(R.id.base_back)
    public void OnClick() {
        finishWitchAnimation();
    }

    /**
     * @param keshiBean 医院全部科室信息
     */
    @Override
    public void getYuyueKeShiList(YuyueKeshiBean keshiBean) {
        if (keshiBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (keshiBean.getData() != null && keshiBean.getData().size() > 0) {
                adapter = new BaseRecyclerRefreshAdapter<YuyueKeshiBean.YuyueKeshiBeanData>(YuyueKeShiActivity.this, keshiBean.getData(), R.layout.yuyue_keshi_item) {
                    @Override
                    public void convert(BaseRecyclerHolder holder, YuyueKeshiBean.YuyueKeshiBeanData item, int position, boolean isScrolling) throws IOException {
                        holder.setText(R.id.yuyue_item, item.getName());
                        mKeshi_Name = item.getName();
                        TextView mItem = holder.getView(R.id.yuyue_item);
                        if (position == getmPosition()) {
                            mItem.setBackgroundResource(R.drawable.keshi_bg);
                        } else {
                            mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                        }
                    }
                };
                mLeftRecyclerView.setAdapter(adapter);
                mKeshilist = keshiBean.getData().get(0).getSubHospitalOffices();
                mAdapter = new BaseRecyclerRefreshAdapter<YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital>(YuyueKeShiActivity.this, keshiBean.getData().get(0).getSubHospitalOffices(), R.layout.yuyue_keshi_item) {
                    @Override
                    public void convert(BaseRecyclerHolder holder, YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital item, int position, boolean isScrolling) throws IOException {
                        holder.setText(R.id.yuyue_item, item.getName());
                        TextView mItem = holder.getView(R.id.yuyue_item);
                        if (position == getmPosition()) {
                            mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                        } else {
                            mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                        }
                    }
                };
                mRightRecyclerView.setAdapter(mAdapter);
                adapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView parent, View view, int position) {
                        mAdapter = new BaseRecyclerRefreshAdapter<YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital>(YuyueKeShiActivity.this, keshiBean.getData().get(position).getSubHospitalOffices(), R.layout.yuyue_keshi_item) {
                            @Override
                            public void convert(BaseRecyclerHolder holder, YuyueKeshiBean.YuyueKeshiBeanData.YuyueKeshiBeanDataSubHospital item, int position, boolean isScrolling) throws IOException {
                                holder.setText(R.id.yuyue_item, item.getName());
                                TextView mItem = holder.getView(R.id.yuyue_item);
                                if (position == getmPosition()) {
                                    mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                                } else {
                                    mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                                }

                            }
                        };
                        mKeshi_Name = keshiBean.getData().get(position).getName();
                        mRightRecyclerView.setAdapter(mAdapter);
                        mKeshilist = keshiBean.getData().get(position).getSubHospitalOffices();
                        mAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(RecyclerView parent, View view, int position) {
                                mAdapter.changeState(position);
                                mAdapter.notifyDataSetChanged();
                                //数据是使用Intent返回
                                Intent intent = new Intent();
                                //把返回数据存入Intent
                                intent.putExtra("keshi", mKeshilist.get(position).getName());
                                intent.putExtra("keshi_name", mKeshi_Name);
                                intent.putExtra("yiyuan", mKeshilist.get(position).getName());
                                intent.putExtra("hospitalId", mKeshilist.get(position).getId());
                                intent.putExtra("keshiId", mKeshilist.get(position).getId());
                                //设置返回数据
                                YuyueKeShiActivity.this.setResult(600, intent);//RESULT_OK为自定义常量
                                //关闭Activity
                                YuyueKeShiActivity.this.finish();
                            }
                        });
                        adapter.changeState(position);
                        adapter.notifyDataSetChanged();
                    }
                });
                mAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView parent, View view, int position) {
                        mAdapter.changeState(position);
                        mAdapter.notifyDataSetChanged();
                        //数据是使用Intent返回
                        Intent intent = new Intent();
                        //把返回数据存入Intent
                        intent.putExtra("keshi", mKeshilist.get(position).getName());
                        intent.putExtra("keshi_name", mKeshi_Name);
                        intent.putExtra("yiyuan", mKeshilist.get(position).getName());
                        intent.putExtra("hospitalId", mKeshilist.get(position).getId());
                        intent.putExtra("keshiId", mKeshilist.get(position).getId());
                        //设置返回数据
                        YuyueKeShiActivity.this.setResult(600, intent);//RESULT_OK为自定义常量
                        //关闭Activity
                        YuyueKeShiActivity.this.finish();
                    }
                });
            }
        } else if (keshiBean.getCode() == 401) {
            if (dialog == null) {
                dialog = new LoginOutDialog(YuyueKeShiActivity.this);
            }
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(YuyueKeShiActivity.this);
                    YuyueKeShiActivity.this.finish();
                    dialog.dismiss();
                }
            });
        } else {
            ToastUtil.showToastINFO(YuyueKeShiActivity.this, keshiBean.getMsg());
        }
    }

    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(YuyueKeShiActivity.this, mErrorMsg);
    }
}
